
package com.shop.cloud.weixin.admin.controller;

import cn.binarywang.wx.miniapp.bean.WxMaDomainAction;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.weixin.admin.config.open.WxOpenConfiguration;
import com.shop.cloud.weixin.common.constant.WxReturnCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序服务器域名
 *
 * @author
 * @date 202-01-25 15:39:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/serveraddress")
@Tag(description = "serveraddress", name = "小程序服务器域名")
public class WxMaServerAddressController {

	/**
	 * 查询第三方平台服务器域名
	 * @return
	 */
	@Operation(summary = "查询第三方平台服务器域名")
	@GetMapping
	@PreAuthorize("@ato.hasAuthority('wxma:serveraddress:index')")
	public R serverAddressGetPlatform() {
		try {
			String map = "{\n" +
					"  \"action\": \"get\"\n" +
					"}";
			String responseContent = WxOpenConfiguration.getOpenService().getWxOpenComponentService().post("https://api.weixin.qq.com/cgi-bin/component/modify_wxa_server_domain",map);
			JSONObject jsonObject = JSONUtil.parseObj(responseContent);
			String errCode = jsonObject.getStr("errcode");
			if(WxReturnCode.SUC_0.getCode().equals(errCode) || StrUtil.isEmpty(errCode)){
				return R.ok(jsonObject);
			}else {
				return R.failed(jsonObject.getStr("errmsg"));
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("查询第三方平台服务器域名：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}
	}

	/**
	 * 查询小程序服务器域名
	 * @return
	 */
	@Operation(summary = "查询小程序服务器域名")
	@GetMapping("/{appId}")
	@PreAuthorize("@ato.hasAuthority('wxma:serveraddress:index')")
	public R serverAddressGet(@PathVariable("appId") String appId) {
		try {
			WxMaDomainAction domainAction = new WxMaDomainAction();
			domainAction.setAction("get");
			WxMaDomainAction wxMaDomainAction = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getSettingService().modifyDomain(domainAction);
			return R.ok(wxMaDomainAction);
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("查询小程序服务器域名：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}
	}

	/**
	 * 设置小程序服务器域名
	 * @return
	 */
	@Operation(summary = "设置小程序服务器域名")
	@PostMapping("/{appId}")
	@PreAuthorize("@ato.hasAuthority('wxma:serveraddress:index')")
	public R serverAddressSet(@PathVariable("appId") String appId, @RequestBody WxMaDomainAction domainAction) {
		try {
			domainAction.setAction("set");
			WxMaDomainAction wxMaDomainAction = WxOpenConfiguration.getOpenService().getWxOpenComponentService().getWxMaServiceByAppid(appId).getSettingService().modifyDomain(domainAction);
			return R.ok(wxMaDomainAction);
		} catch (WxErrorException e) {
			e.printStackTrace();
			log.error("设置小程序服务器域名：", e);
			return WxReturnCode.wxErrorExceptionHandler(e);
		}
	}
}
